
package Gui;

import Oracle.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Alonso
 */
public class FacturacionGUI extends javax.swing.JFrame {

    /** Creates new form FacturacionGUI */
    public FacturacionGUI() throws SQLException {
        initComponents();
        this.setLocationRelativeTo(null);
        buttonVer.setVisible(false);
        llenarTablaFacturas();
    }

    //---------------------LLENAR TABLAS--------------------------------------//
    
    private void llenarTablaFacturas() throws SQLException{
        DefaultTableModel table = new DefaultTableModel();
        table.addColumn("ID Number");
        table.addColumn("Cliente");
        table.addColumn("Fecha");
        table.addColumn("Monto");
        
        Connection conexion = OracleMethods.oracleConnection();
        Statement sentencia = conexion.createStatement();
        ResultSet resultado = sentencia.executeQuery("SELECT * FROM ao_factura");
        
         while(resultado.next()) { 
            Vector row = new Vector();
            row.add(resultado.getInt("factura_PK"));
            row.add(resultado.getString("rif_fk"));
            row.add(resultado.getString("fecha_factura"));
            row.add(resultado.getString("monto"));
            table.addRow(row);
         }
            
            this.tableFacturas.setModel(table);
            this.tableFacturas.setVisible(true);
            
            sentencia.close();
            resultado.close();
            conexion.close();
        
    }
    
    private void llenarTablaDetalles(String idFactura) throws SQLException{
        this.tableDetalle.removeAll();
        int repeticion = 1;
        
        DefaultTableModel table = new DefaultTableModel();
        Connection conexion = OracleMethods.oracleConnection();
        Statement sentencia = conexion.createStatement();
        ResultSet resultado = sentencia.executeQuery("SELECT * FROM ao_detalle WHERE factura_pk_fk = " + idFactura);
        buttonVer.setVisible(true);
        
         while(resultado.next()) { 
            Vector row = new Vector();
            if(resultado.getString("Fecha_venta_fk") != null){
                table.addColumn("Tipo");
                table.addColumn("Fecha de Venta");
                table.addColumn("Kiosco");
                table.addColumn("Monto");
                row.add("Venta de Kiosco");
                row.add(resultado.getString("fecha_venta_fk"));
                row.add(resultado.getString("kiosco_fk"));
                row.add(resultado.getString("monto"));
                buttonVer.setText("Ver Ventas");
            }
            if(resultado.getString("Alquiler_fk") != null){
                table.addColumn("Nro");
                table.addColumn("Kiosco");
                table.addColumn("Monto");
                row.add("Alquiler");
                row.add(resultado.getString("alquiler_fk"));
                row.add(resultado.getString("kiosco_fk"));
                row.add(resultado.getString("monto"));
                buttonVer.setText("Ver Alquileres");
            }
            if(resultado.getString("Pedido_fk") != null){
                if (repeticion == 1){
                    table.addColumn("Tipo");
                    table.addColumn("Nro");
                    table.addColumn("Monto");
                }
                row.add("Valla");
                row.add(resultado.getString("pedido_fk"));
                row.add(resultado.getString("monto"));
                buttonVer.setText("Ver Pedidos");
                repeticion++;
            }
            if(resultado.getString("CI_fk") != null){
                table.addColumn("Tipo");
                table.addColumn("Fecha");
                table.addColumn("Empleado");
                table.addColumn("Monto");
                row.add("Mantenimiento");
                row.add(resultado.getString("fecha_mant_fk"));
                row.add(resultado.getString("ci_fk"));
                row.add(resultado.getString("monto"));
                buttonVer.setText("Mantenimientos");
            }
            table.addRow(row);
         }
            
            this.tableDetalle.setModel(table);
            this.tableDetalle.setVisible(true);
            
            sentencia.close();
            resultado.close();
            conexion.close();
        
    }
    

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonAtras = new javax.swing.JButton();
        jPanel11 = new javax.swing.JPanel();
        jScrollPane6 = new javax.swing.JScrollPane();
        tableDetalle = new javax.swing.JTable();
        jPanel15 = new javax.swing.JPanel();
        jScrollPane9 = new javax.swing.JScrollPane();
        tableFacturas = new javax.swing.JTable();
        buttonPago = new javax.swing.JButton();
        buttonVer = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        buttonAtras.setFont(new java.awt.Font("Tahoma", 0, 14));
        buttonAtras.setText("Atras");
        buttonAtras.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonAtrasActionPerformed(evt);
            }
        });

        jPanel11.setBorder(javax.swing.BorderFactory.createTitledBorder("Detalles"));

        tableDetalle.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Tipo", "Nro", "Fecha", "Monto"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tableDetalle.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableDetalleMouseClicked(evt);
            }
        });
        jScrollPane6.setViewportView(tableDetalle);

        javax.swing.GroupLayout jPanel11Layout = new javax.swing.GroupLayout(jPanel11);
        jPanel11.setLayout(jPanel11Layout);
        jPanel11Layout.setHorizontalGroup(
            jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel11Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane6, javax.swing.GroupLayout.DEFAULT_SIZE, 483, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel11Layout.setVerticalGroup(
            jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel11Layout.createSequentialGroup()
                .addComponent(jScrollPane6, javax.swing.GroupLayout.DEFAULT_SIZE, 330, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel15.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        tableFacturas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "ID Number", "Fecha", "Monto"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tableFacturas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableFacturasMouseClicked(evt);
            }
        });
        jScrollPane9.setViewportView(tableFacturas);

        javax.swing.GroupLayout jPanel15Layout = new javax.swing.GroupLayout(jPanel15);
        jPanel15.setLayout(jPanel15Layout);
        jPanel15Layout.setHorizontalGroup(
            jPanel15Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel15Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane9, javax.swing.GroupLayout.DEFAULT_SIZE, 459, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel15Layout.setVerticalGroup(
            jPanel15Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel15Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane9, javax.swing.GroupLayout.DEFAULT_SIZE, 382, Short.MAX_VALUE)
                .addContainerGap())
        );

        buttonPago.setFont(new java.awt.Font("Tahoma", 0, 14));
        buttonPago.setText("Pagos");
        buttonPago.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonPagoActionPerformed(evt);
            }
        });

        buttonVer.setFont(new java.awt.Font("Tahoma", 0, 14));
        buttonVer.setText("Mantenimientos");
        buttonVer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonVerActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel15, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(buttonVer, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(buttonPago, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonAtras, javax.swing.GroupLayout.DEFAULT_SIZE, 173, Short.MAX_VALUE))
                    .addComponent(jPanel11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel15, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jPanel11, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(5, 5, 5)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(buttonVer, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(buttonPago, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(buttonAtras, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void buttonPagoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonPagoActionPerformed
        try {
            
            int row = this.tableFacturas.getSelectedRow();
            String idFactura = (String) this.tableFacturas.getModel().getValueAt(row, 0).toString();
            String monto = "";
            int i = 0;
            while(monto != null){
                monto = this.tableFacturas.getModel().getColumnName(i).toString();
                if(monto.equals("Monto")){
                    monto = (String) this.tableFacturas.getModel().getValueAt(row, i).toString();
                    break;
                }
                i++;
            }
            
            new PagosGUI(this,true,idFactura,monto).setVisible(true);
            
        } catch (SQLException ex) {
            Logger.getLogger(FacturacionGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_buttonPagoActionPerformed

private void buttonVerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonVerActionPerformed
    try {
    if(buttonVer.getText().equals("Mantenimientos")){
        new ManejoMantenimientoGUI("M").setVisible(true);
    }
    if(buttonVer.getText().equals("Ver Ventas")){
        new VentaKioscosGUI().setVisible(true); 
    }
    if(buttonVer.getText().equals("Ver Alquileres")){
        new PublicidadGUI().setVisible(true);
    }
    if(buttonVer.getText().equals("Ver Pedidos")){
       new ManejoMantenimientoGUI("V").setVisible(true);
    }
    } catch (SQLException ex) {
                Logger.getLogger(FacturacionGUI.class.getName()).log(Level.SEVERE, null, ex);
            }
}//GEN-LAST:event_buttonVerActionPerformed

private void buttonAtrasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAtrasActionPerformed
this.dispose();
}//GEN-LAST:event_buttonAtrasActionPerformed

private void tableFacturasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableFacturasMouseClicked
        try {
            int row = this.tableFacturas.getSelectedRow();
            String idFactura = (String) this.tableFacturas.getModel().getValueAt(row, 0).toString();
            llenarTablaDetalles(idFactura);
            
            
        } catch (SQLException ex) {
            Logger.getLogger(FacturacionGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_tableFacturasMouseClicked

private void tableDetalleMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableDetalleMouseClicked
// TODO add your handling code here:
}//GEN-LAST:event_tableDetalleMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FacturacionGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FacturacionGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FacturacionGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FacturacionGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    new FacturacionGUI().setVisible(true);
                } catch (SQLException ex) {
                    Logger.getLogger(FacturacionGUI.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonAtras;
    private javax.swing.JButton buttonPago;
    private javax.swing.JButton buttonVer;
    private javax.swing.JPanel jPanel11;
    private javax.swing.JPanel jPanel15;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JScrollPane jScrollPane9;
    private javax.swing.JTable tableDetalle;
    private javax.swing.JTable tableFacturas;
    // End of variables declaration//GEN-END:variables
}
